<template>
  <div class="container">
    <div>
      <span @click="tui()">X</span>
      <el-input v-model="input" />
      <button @click="fa()">发布</button>
    </div>
    <div style="display: flex;">
      <div style="border: 1px solid #ccc;width: 600px;">
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editorRef"
          :defaultConfig="toolbarConfig"
          :mode="mode"
        />
        <Editor
          style="height: 500px; overflow-y: hidden;"
          v-model="valueHtml"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
        />
      </div>
      <div style="width: 400px;">{{ input }}</div>
    </div>
  </div>
</template>

<script lang="ts">
import { useRoute } from 'vue-router'
import '@wangeditor/editor/dist/css/style.css'
import { ElMessage } from 'element-plus'
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { yon,xiugai } from '../../api'
export default {
  components: { Editor, Toolbar },
  setup() {
    const Route = useRoute()
    const list = ref()
    const input = ref('')
    const id = Route.params.id
   onMounted(()=>{
      yon({id}).then(res=>{
        console.log(res);
        input.value=res.data.data.username
      })
   })
    // 页面管理修改
    const fa = () => {
      console.log(input);
        xiugai({input,id}).then(res=>{
          console.log(res);
          ElMessage({
          message: '修改成功',
          type: 'success',
        })
        })
    }

    const editorRef = shallowRef()

    // 内容 HTML
    const valueHtml = ref('<p>hello</p>')

    // 模拟 ajax 异步获取内容
    onMounted(() => {
      setTimeout(() => {
        valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>'
      }, 1500)
    })

    const toolbarConfig = {}
    const editorConfig = { placeholder: '请输入内容...' }

    // 组件销毁时，也及时销毁编辑器
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })

    const handleCreated = (editor: any) => {
      editorRef.value = editor // 记录 editor 实例，重要！
    }
    const tui = () => {
      window.location.href = "/Pagemanagement"
    }
    return {
      list,
      editorRef,
      valueHtml,
      mode: 'default', // 或 'simple'
      toolbarConfig,
      editorConfig,
      handleCreated,
      input,
      fa,
      tui
    }
  }
}
</script>

<style scoped lang="scss">
</style>
